package com.sunl.mall.ali.mq.autoconfigure.config;


import com.sunl.mall.common.util.StringUtil;
import org.springframework.beans.factory.InitializingBean;

import java.util.Arrays;

/**
 * rocketmq 日志配置
 */
public class RocketMqLoggerConfig implements InitializingBean {
    private static final String[] LEVEL_ARRAY = new String[]{"ERROR", "WARN", "INFO", "DEBUG"};

    private String logRoot;
    private String logLevel;
    private int logMaxIndex;

    public void setLogRoot(String logRoot) {
        this.logRoot = logRoot;
    }

    public void setLogLevel(String logLevel) {
        this.logLevel = logLevel;
    }

    public void setLogMaxIndex(int logMaxIndex) {
        this.logMaxIndex = logMaxIndex;
    }

    @Override
    public void afterPropertiesSet() {
        // 日志保存路径
        if (StringUtil.isNotBlank(this.logRoot)) {
            System.setProperty("ons.client.logRoot", this.logRoot);
        }
        // 日志级别
        if (StringUtil.isNotBlank(this.logLevel)) {
            if (!Arrays.asList(LEVEL_ARRAY).contains(this.logLevel.toUpperCase())) {
                throw new IllegalArgumentException("Mq Log level must be [ERROR, WARN, INFO, DEBUG]");
            }
            System.setProperty("ons.client.logLevel", this.logLevel);
        }
        // 保存历史日志文件的最大个数
        if (this.logMaxIndex <= 0 || this.logMaxIndex > 10) {
            throw new IllegalArgumentException("Mq Log max index must be Between 1 and 10");
        }
        System.setProperty("ons.client.logFileMaxIndex", this.logMaxIndex + "");
    }
}
